﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RAP.Framework.Libary.Utils.Interfaces;

namespace RAP.Framework.Libary.Utils
{
	public static partial class Extensions
	{
		#region IKeyValuePair
		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static bool ToBoolean ( this IKeyValuePair obj, object key, bool defaultValue = false )
		{
			if ( obj == null || key.IsNullOrEmpty ( ) )
			{
				return defaultValue;
			}
			return obj[ key ].ToBooleanValue ( defaultValue );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static string ToString ( this IKeyValuePair obj, object key, string defaultValue = null )
		{
			if ( obj == null || key.IsNullOrEmpty ( ) )
			{
				return defaultValue;
			}
			object value = obj[ key ];
			if ( value.IsNullOrEmpty ( ) )
			{
				return defaultValue;
			}
			return value.ToString ( );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static int ToInt32 ( this IKeyValuePair obj, object key, int defaultValue = 0 )
		{
			if ( obj == null || key.IsNullOrEmpty ( ) )
			{
				return defaultValue;
			}
			return obj[ key ].ToInt32Value ( defaultValue );
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static DateTime? ToDateTime ( this IKeyValuePair obj, object key, DateTime? defaultValue = null )
		{
			if ( obj == null || key.IsNullOrEmpty ( ) )
			{
				return defaultValue;
			}
			return obj.ToString ( key, null ).ToDateTimeValue ( defaultValue );
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="obj"></param>
		/// <param name="key"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static IDictionary ToDictionary ( this IKeyValuePair obj, object key, IDictionary defaultValue = null )
		{
			if ( obj == null || key.IsNullOrEmpty ( ) )
			{
				return defaultValue;
			}
			object value = obj[ key ];
			if ( value == null )
			{
				return defaultValue;
			}
			return value as IDictionary;
		}
		#endregion
	}
}
